package com.smedi.ismedi.dangeroussourcemanage.core.domain.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceLibrary;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceManage;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceLibraryRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.service.DangerousSourceManageDomainService;
import com.smedi.ismedi.dangeroussourcemanage.core.infrastructure.constant.DangerousSourceManageError;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.model.DangerousSourceManageEntity;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.mybatis.mapper.DangerousSourceManageMapper;
import com.smedi.ismedi.web.starter.exception.LogicException;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务服务实体类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Component
@AllArgsConstructor
public class DangerousSourceManageDomainServiceImpl extends ServiceImpl<DangerousSourceManageMapper, DangerousSourceManageEntity> implements DangerousSourceManageDomainService, IService<DangerousSourceManageEntity> {
    private final DangerousSourceManageMapper dangerousSourceManageMapper;
    private final DangerousSourceLibraryRepository dangerousSourceLibraryRepository;

    private final DozerUtil dozerUtil;

    @Override
    public DangerousSourceManage doInsert(DangerousSourceManage dangerousSourceManage) {
        DangerousSourceManage dangerousSourceManageReturn;
        DangerousSourceManageEntity dangerousSourceManageEntity = dozerUtil.convertor(dangerousSourceManage, DangerousSourceManageEntity.class);

        DangerousSourceLibrary dangerousSourceLibrary = dangerousSourceLibraryRepository.selectDangerousSourceLibraryById(dangerousSourceManage.getLibraryId());
        if (dangerousSourceLibrary == null) {
            throw LogicException.raise(DangerousSourceManageError.Hazard_Source_Library_Not_Exist);
        }

        this.getBaseMapper().insert(dangerousSourceManageEntity);

        dangerousSourceManageReturn = dozerUtil.convertor(dangerousSourceManageEntity, DangerousSourceManage.class);

        return dangerousSourceManageReturn;
    }

    @Override
    public DangerousSourceManage doUpdate(DangerousSourceManage dangerousSourceManage) {
        DangerousSourceManageEntity dangerousSourceManageEntity = dozerUtil.convertor(dangerousSourceManage, DangerousSourceManageEntity.class);

        DangerousSourceLibrary dangerousSourceLibrary = dangerousSourceLibraryRepository.selectDangerousSourceLibraryById(dangerousSourceManage.getLibraryId());
        if (dangerousSourceLibrary == null) {
            throw LogicException.raise(DangerousSourceManageError.Hazard_Source_Library_Not_Exist);
        }

        this.getBaseMapper().updateById(dangerousSourceManageEntity);

        return dozerUtil.convertor(dangerousSourceManageEntity, DangerousSourceManage.class);
    }

    @Override
    public Long doDeleteById(List<Long> dangerousSourceIds) {
        return Long.valueOf(dangerousSourceManageMapper.deleteBatchIds(dangerousSourceIds));
    }
}
